/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-05-27
*/
package com.rzico.basics.service;

import com.rzico.base.BaseMapper;
import com.rzico.base.impl.BaseServiceImpl;
import com.rzico.basics.entity.Designer;
import com.rzico.basics.entity.Enterprise;
import com.rzico.basics.entity.WxPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import com.rzico.basics.mapper.WxPageMapper;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <pre>
 * 页面管理业务类
 * </pre>
 *
 * @author Rzico Boot
 * @version 1.0
 */
@Service
public class WxPageService extends BaseServiceImpl<WxPage, String> {

    @Autowired
    private WxPageMapper wxPageMapper;

    @Autowired  @Lazy
    private DesignerService designerService;

    @Override
    public BaseMapper<WxPage, String> getMapper() {
        return wxPageMapper;
    }

    public WxPage getHome(Enterprise enterprise) {
        WxPage wxPage = new WxPage();
        wxPage.setPageType(0);
        wxPage.setEnterpriseId(enterprise.getId());
        List<WxPage> wxPageList = select(wxPage);
        if (wxPageList.size()>0) {
            return wxPageList.get(0);
        } else {
            return initHome(enterprise);
        }
    }

    @Transactional(rollbackFor = Exception.class)
    public WxPage initHome(Enterprise enterprise) {
        WxPage wxPage = new WxPage();
        wxPage.setPageType(0);
        wxPage.setEnterpriseId(enterprise.getId());
        wxPage.setName("首页");
        wxPage.setDescription("商城首页（不能删除）");
        wxPage.setThumbnial(enterprise.getLogo());
        wxPage.setOrders(99);
        super.insertUseGeneratedKeys(wxPage);

        //对历史记录进行处理
        Map<String,Object> params = new HashMap<>();
        params.put("enterpriseId",enterprise.getId());
        List<Designer> designers = designerService.selectList(params);
        for (Designer designer:designers) {
            if (designer.getPageId()==null) {
                designer.setPageId(wxPage.getId());
                designerService.updateByPrimaryKeySelective(designer);
            }
        }

        return wxPage;
    }

}
